package com.wxl.common.util

import Decoder.BASE64Decoder
import Decoder.BASE64Encoder
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.SecretKeySpec

class EncryptUtils {
    companion object {

        val key = "jadl12345678901234"
        val mbolsk = "jadl12345678912345678912"

        /**
         * 电子雷管解析
         *
         * @param content
         * @param key
         * @return
         */
        fun decodeNoURL(content: String?, key: String?): String? {
            var content = content
            try {
                content = decode(content, key!!)
            } catch (e: Exception) {
                content = ""
                e.printStackTrace()
            }
            return content
        }


        @Throws(java.lang.Exception::class)
        fun endocde(str: String, key: String): String? {
            var tmp = ByteArray(24)
            tmp = key.toByteArray()
            val k: SecretKey = SecretKeySpec(tmp, "DESede")
            val cipher = Cipher.getInstance("DESede")
            cipher.init(Cipher.ENCRYPT_MODE, k)
            val waitforencode_bytes = str.toByteArray(charset("UTF-8"))
            val encode_bytes = cipher.doFinal(waitforencode_bytes)
            return BASE64Encoder().encode(encode_bytes).toString()
        }


        @Throws(java.lang.Exception::class)
        fun decode(str: String?, key: String): String? {
            val decode_byte: ByteArray = BASE64Decoder().decodeBuffer(str)
            var tmp = ByteArray(24)
            tmp = key.toByteArray()
            val k: SecretKey = SecretKeySpec(tmp, "DESede")
            val cipher = Cipher.getInstance("DESede")
            cipher.init(Cipher.DECRYPT_MODE, k)
            val decode_bytes = cipher.doFinal(decode_byte)
            return String(decode_bytes, charset("UTF-8"))
        }

        fun endocdeNoURL(inObj: String?, key: String?): String? {
            var inObj = inObj
            try {
                inObj = endocde(inObj!!, key!!)
            } catch (e: java.lang.Exception) {
                inObj = ""
                e.printStackTrace()
                Loog.e("加密失败 =  " + e.message)
            }
            return inObj
        }



    }
}